package com.GLM.Viewer.Party;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Entity.MeetDataEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class PartyJourneyActivity extends Activity {
	private final static int PARTYLIST_LOADING = 111;
	private final static int PARTYLIST_LONDED = 222;

	private String[] itemKeys = new String[] { "partyName", "partyTime", "personCount" };
	private int[] itemIds = new int[] { R.id.party_journey_partyname, R.id.party_journey_partytime, R.id.party_journey_joincount };

	private int mUserId;
	private ProgressBar mProgressBar;
	private ListView mListView;
	private SimpleAdapter mAdapter;
	private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();
	private List<MeetDataEntity> allParty = new ArrayList<MeetDataEntity>();
	private int screenWidth;
	private int screenHeight;
	
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Thread thread;
			
			switch (msg.what) {
			case PARTYLIST_LOADING:
				mProgressBar.setVisibility(View.VISIBLE);
				thread = new Thread(new Runnable() {
					public void run() {
						
						initPartyList();
						mAdapter = new SimpleAdapter(PartyJourneyActivity.this, mItems, R.layout.view_party_journey_listitem, itemKeys, itemIds);
						mHandler.sendEmptyMessage(PARTYLIST_LONDED);
					}

				});
				thread.start();
				break;

			case PARTYLIST_LONDED:
				mProgressBar.setVisibility(View.GONE);
				if(mAdapter != null && mItems.size()>0){
					mListView.setAdapter(mAdapter);
					bindItemClickedEvent();
				}else{
					Paint paint = new Paint();
					paint.setTextSize(20);
					paint.setAntiAlias(true);
					String str = getString(R.string.journey_no_journey);
					Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.glm_background);
					Canvas canvas = new Canvas(bitmap);
					canvas.drawText(str,screenWidth/2-str.length()*9 , screenHeight/2, paint);
					RelativeLayout layout = (RelativeLayout)findViewById(R.id.partyjourney_layout);
					Drawable drawable = new BitmapDrawable(bitmap);
					layout.setBackgroundDrawable(drawable);
				}
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_party_journey);
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		screenHeight = getWindowManager().getDefaultDisplay().getHeight();

		mProgressBar = (ProgressBar) findViewById(R.id.partylist_progressbar);
		mListView = (ListView) findViewById(R.id.party_journey_listview);
		mUserId = UserInfoDAO.getUserInfo(this).getUserId();
		mHandler.sendEmptyMessage(PARTYLIST_LOADING);
	}

	private void initPartyList() {
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			jsonData.put(Constant.PARAM_USERID, mUserId);
			jsonData.put("ways", 3);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_GETPARTYJOURNEY, jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray(Constant.RESP_PATTY_LIST);
					initPartyListItemFromJSONObject(jsonArray);
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	private void initPartyListItemFromJSONObject(JSONArray jsonArray) throws JsonSyntaxException, JSONException {
		Gson gson = new Gson();
		String num = getString(R.string.person);
		for (int i = 0; i < jsonArray.length(); i++) {
			MeetDataEntity party = gson.fromJson(jsonArray.getJSONObject(i).toString(), MeetDataEntity.class);
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("partyName", party.getMeet_name());
			item.put("partyTime", party.getMeet_createDate());
			item.put("personCount", party.getMeet_enlist().length + num);
			mItems.add(item);
			allParty.add(party);
		}

	}

	private void bindItemClickedEvent() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// ListView listView = (ListView) parent;
				// Log.v("Test", "id = " + id + "(" + listView.getItemAtPosition(position).toString() + ")");
				MeetDataEntity party = allParty.get((int) id);
				Intent intent = new Intent(PartyJourneyActivity.this, PartyDetailActivity.class);
				intent.putExtra("party", party);
				intent.putExtra("ParentActivity", "PartyJourneyActivity");
				startActivity(intent);
			}
		});
	}

	public void goBackButton_OnClick(View v) {
		NavUtils.navigateUpTo(PartyJourneyActivity.this, new Intent(PartyJourneyActivity.this,PartyMainActivity.class));
	}
	
}
